package com.bci.pwtz.service.impl;

import java.text.MessageFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.bci.pwtz.common.PwtzConstants;
import com.bci.pwtz.common.util.PaginationList;
import com.bci.pwtz.common.util.PwtzUtils;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.exception.PwtzException;
import com.bci.pwtz.mysql.dao.CompanyUserMapper;
import com.bci.pwtz.mysql.dao.CompanyUserRoleMapper;
import com.bci.pwtz.mysql.dao.RandomCodeMapper;
import com.bci.pwtz.mysql.dao.UserCompanyRefMapper;
import com.bci.pwtz.mysql.model.CompanyUser;
import com.bci.pwtz.mysql.model.CompanyUserRole;
import com.bci.pwtz.mysql.model.RandomCode;
import com.bci.pwtz.mysql.model.UserCompanyRef;
import com.bci.pwtz.service.CompanyService;
import com.bci.pwtz.service.CompanyUserService;

/**
 * 一句话描述 <br/>
 * <p>
 * Description:<br/>
 * <p>
 * TODO 详细描述
 * 
 * <p>
 * For Examples <br/>
 * 
 * <PRE>
 *      TODO 代码使用示例
 * </PRE>
 * <p>
 */
@Service
public class CompanyUserServiceImpl implements CompanyUserService
{

    private CompanyUserMapper companyUserMapper;
    
    private RandomCodeMapper randomCodeMapper;
    
    private UserCompanyRefMapper userCompanyRefMapper;
    
    private CompanyUserRoleMapper companyUserRoleMapper;
    
    private CompanyService companyService;
    
    private JdbcTemplate jdbcTemplate;
    
    
    /**
     * @return the companyUserMapper
     */
    public CompanyUserMapper getCompanyUserMapper()
    {
        return companyUserMapper;
    }

    /**
     * @param companyUserMapper the companyUserMapper to set
     */
    public void setCompanyUserMapper(CompanyUserMapper companyUserMapper)
    {
        this.companyUserMapper = companyUserMapper;
    }

    /**
     * @return the randomCodeMapper
     */
    public RandomCodeMapper getRandomCodeMapper()
    {
        return randomCodeMapper;
    }

    /**
     * @param randomCodeMapper the randomCodeMapper to set
     */
    public void setRandomCodeMapper(RandomCodeMapper randomCodeMapper)
    {
        this.randomCodeMapper = randomCodeMapper;
    }
    
    /**
     * @return the userCompanyRefMapper
     */
    public UserCompanyRefMapper getUserCompanyRefMapper()
    {
        return userCompanyRefMapper;
    }

    /**
     * @param userCompanyRefMapper the userCompanyRefMapper to set
     */
    public void setUserCompanyRefMapper(UserCompanyRefMapper userCompanyRefMapper)
    {
        this.userCompanyRefMapper = userCompanyRefMapper;
    }

    /**
     * @return the companyUserRoleMapper
     */
    public CompanyUserRoleMapper getCompanyUserRoleMapper()
    {
        return companyUserRoleMapper;
    }

    /**
     * @param companyUserRoleMapper the companyUserRoleMapper to set
     */
    public void setCompanyUserRoleMapper(CompanyUserRoleMapper companyUserRoleMapper)
    {
        this.companyUserRoleMapper = companyUserRoleMapper;
    }

    /**
     * @return the companyService
     */
    public CompanyService getCompanyService()
    {
        return companyService;
    }

    /**
     * @param companyService the companyService to set
     */
    public void setCompanyService(CompanyService companyService)
    {
        this.companyService = companyService;
    }

    private static final Logger _logger = Logger.getLogger(CompanyUserServiceImpl.class);

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param user
     * 
     * @return
     * 
     * @throws CourtDatabaseException
     * 
     * @see com.bcinfo.alumni.service.BranchService#add(com.bcinfo.alumni.model.CompanyUser)
     */
    @Override
    public int addUserCompanyRef(UserCompanyRef ref) throws PwtzDatabaseException
    {
        int ret = 0;
        if (_logger.isDebugEnabled())
        {
            _logger.debug("add:UserCompanyRef=" + ref.toString());
        }
        if (ref == null)
        {
            if (_logger.isDebugEnabled())
            {
                _logger.debug("add,UserCompanyRef is null");
            }
            return 0;
        }
        try
        {
            ret = userCompanyRefMapper.insertSelective(ref);
        }
        catch (DataAccessException _e)
        {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new PwtzDatabaseException();
        }
        return ret;
    }

    @Override
    public int addCompanyUserRole(CompanyUserRole ref) throws PwtzDatabaseException
    {
        int ret = 0;
        if (_logger.isDebugEnabled())
        {
            _logger.debug("add:CompanyUserRole=" + ref.toString());
        }
        if (ref == null)
        {
            if (_logger.isDebugEnabled())
            {
                _logger.debug("add,UserCompanyRef is null");
            }
            return 0;
        }
        try
        {
        	int usercount = companyUserRoleMapper.selectCountByUserIdAndCompanyId(ref.getCompanyUserId(), ref.getCompanyRoleId());
        	//System.out.println("companyUserId="+companyUserId+",companyRoleId="+companyRoleId+",usercount="+usercount);
        	if(usercount == 0){
        		ret = companyUserRoleMapper.insertSelective(ref);
        	}
        }
        catch (DataAccessException _e)
        {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new PwtzDatabaseException();
        }
        return ret;
    }
    
    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param userId
     * 
     * @return
     * 
     * @throws CourtDatabaseException
     * 
     * @see com.bcinfo.alumni.service.BranchService#delete(long)
     */
    @Override
    public int delete(Long userId) throws PwtzDatabaseException
    {
        int result = 0;
        if (_logger.isInfoEnabled())
        {
            _logger.info("delete:userId=" + userId);
        }
        try
        {
            result = companyUserMapper.deleteByPrimaryKey(userId);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return result;
    }

    /*
     * (non-Javadoc)
     * Description: <br/>
     * 
     * @param userId
     * 
     * @return
     * 
     * @throws CourtDatabaseException
     * 
     * @see com.bcinfo.alumni.service.BranchService#load(long)
     */
    @Override
    public CompanyUser load(Long userId) throws PwtzDatabaseException
    {
        if (_logger.isInfoEnabled())
        {
            _logger.info("load:userId=" + userId);
        }
        CompanyUser user = null;
        try
        {
            user = companyUserMapper.selectByPrimaryKey(userId);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (user != null)
        {
            _logger.debug("load:result user=" + user.toString());
        }
        return user;
    }
    
    @Override
    public CompanyUser findUserInfo(String tel,String userPwd) throws PwtzDatabaseException
    {
        if (_logger.isInfoEnabled())
        {
            _logger.info("findUserInfo:tel=" + tel+"|userPwd=" + userPwd);
        }
        CompanyUser user = null;
        try
        {
                user = companyUserMapper.selectUserByUserInfo(tel,userPwd);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return user;
    }

    @Override
	public int update(CompanyUser user) throws PwtzDatabaseException {
	    int result = 0;
	    if (_logger.isDebugEnabled())
        {
            _logger.debug("add:user=" + user.toString());
        }
        if (user == null)
        {
            if (_logger.isDebugEnabled())
            {
                _logger.debug("add,user is null");
            }
            return 0;
        }
        try
        {
            result=companyUserMapper.updateByPrimaryKeySelective(user);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
		return result;
	}

    /* (non-Javadoc)
     * Description: <br/> 
     *
     * @param deviceId
     * @return
     * @throws PwtzDatabaseException 
     * @see com.bci.pwtz.service.CompanyUserService#clearDeviceId(java.lang.String)
     */
    @Override
    public int clearDeviceId(String deviceId) throws PwtzDatabaseException
    {
        int result = 0;
        if (_logger.isInfoEnabled())
        {
            _logger.info("clearDeviceId:deviceId=" + deviceId);
        }
        try
        {
            result = companyUserMapper.clearDeviceId(deviceId);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return result;
    }
    
    @Override
    public CompanyUser register(CompanyUser user) throws PwtzDatabaseException
    {
        if (_logger.isDebugEnabled())
        {
            _logger.debug("register:user=" + user.toString());
        }
        if (user == null)
        {
            if (_logger.isDebugEnabled())
            {
                _logger.debug("register,user is null");
            }
            return null;
        }
        try
        {
            user.setCreateTime(new Date());
            companyUserMapper.insertSelective(user);
        }
        catch (DataAccessException _e)
        {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new PwtzDatabaseException();
        }
        return user;
    }
    
    @Override
    public String createRandomCode(Long companyId,Long companyRoleId) throws PwtzDatabaseException
    {
        int ret = 0;
        if (_logger.isDebugEnabled())
        {
            _logger.debug("add:companyId=" + companyId+"|companyRoleId="+companyRoleId);
        }
        String randomCode = PwtzUtils.getRandomNumber(6);
        try
        {
            ret = randomCodeMapper.insertDistinct(null,companyId, companyRoleId, randomCode, PwtzConstants.INVITATION_CODE_VALID);
            if(ret==0){
                randomCode = PwtzUtils.getRandomNumber(6);
                ret = randomCodeMapper.insertDistinct(null,companyId, companyRoleId, randomCode, PwtzConstants.INVITATION_CODE_VALID);
                if(ret==0){
                    randomCode = PwtzUtils.getRandomNumber(6);
                    ret = randomCodeMapper.insertDistinct(null,companyId, companyRoleId, randomCode, PwtzConstants.INVITATION_CODE_VALID);
                    if(ret==0){
                        randomCode="";
                    }
                }
            }
        }
        catch (DataAccessException _e)
        {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new PwtzDatabaseException();
        }
        return randomCode;
    }

    @Override
    public void deleteExpireData() throws PwtzDatabaseException
    {
        try
        {
            randomCodeMapper.deleteExpireData();
        }
        catch (DataAccessException _e)
        {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new PwtzDatabaseException();
        }
        
    }
    
    @Override
    public int selectCountByName(String userName) throws PwtzDatabaseException
    {
        int result = 0;
        if (_logger.isInfoEnabled())
        {
            _logger.info("selectCountByName:userName=" + userName);
        }
        try
        {
            result = companyUserMapper.selectCountByName(userName);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return result;
    }
    
    @Override
    public int selectCountByMobile(String mobile) throws PwtzDatabaseException
    {
        int result = 0;
        if (_logger.isInfoEnabled())
        {
            _logger.info("selectCountByName:mobile=" + mobile);
        }
        try
        {
            result = companyUserMapper.selectCountByMobile(mobile);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return result;
    }
    
    @Override
    public CompanyUser selectUser(String userName,String mobile) throws PwtzDatabaseException
    {
        if (_logger.isInfoEnabled())
        {
            _logger.info("selectCountByName:mobile=" + mobile);
        }
        try
        {
            return companyUserMapper.selectUser(userName,mobile);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
    }
    
    @Override
    public RandomCode selectRandomCode(String code) throws PwtzDatabaseException
    {
        if (_logger.isInfoEnabled())
        {
            _logger.info("selectRandomCode:code=" + code);
        }
        RandomCode randomCode = null;
        try
        {
            randomCode = randomCodeMapper.selectRandomCode(code);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (randomCode != null)
        {
            _logger.debug("load:result randomCode=" + randomCode.toString());
        }
        return randomCode;
    }

    @Override
    public int selectCountByPassword(Long companyUserId,String pwd) throws PwtzDatabaseException
    {
        int result = 0;
        if (_logger.isInfoEnabled())
        {
            _logger.info("selectCountByPassword=" + pwd);
        }
        try
        {
            result = companyUserMapper.selectCountByPassword(pwd, companyUserId);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return result;
    }
    
    @Override
    public int updatePwdByMobile(String pwd,String mobile) throws PwtzDatabaseException
    {
        int result = 0;
        if (_logger.isInfoEnabled())
        {
            _logger.info("updatePwdByMobile:pwd=" + pwd + ",mobile=" + mobile);
        }
        try
        {
            result = companyUserMapper.updatePwdByMobile(pwd, mobile);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return result;
    }

    @Override
    public Map<Long, Integer> getUserCompanyRef(long companyUserID) throws PwtzException
    {
        Map<Long,Integer> returnMap = new HashMap<Long, Integer>();
        //1为已加入 2为待审核
        final String sql = "SELECT COMPANY_ID,1 AS STATUS FROM T_USER_COMPANY_REF WHERE COMPANY_USER_ID=? UNION ALL SELECT COMPANY_ID,2 AS STATUS FROM T_COMPANY_USER_AUDIT WHERE COMPANY_USER_ID=? AND RESULT=1";
        try
        {
            List<Map<String,Object>> retVal = jdbcTemplate.queryForList(sql, companyUserID,companyUserID);
            for(Map<String,Object> m : retVal)
            {
                long companyID = Long.parseLong(m.get("COMPANY_ID").toString());
                int status = Integer.parseInt(m.get("STATUS").toString());
                if(!returnMap.containsKey(companyID))
                {
                    returnMap.put(companyID, status);
                }
            }
            return returnMap;
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        catch (Exception e) 
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzException();
        }
    }

    public JdbcTemplate getJdbcTemplate()
    {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate)
    {
        this.jdbcTemplate = jdbcTemplate;
    }
    
    @Override
    public PaginationList<CompanyUser> selectUserByRole(long companyRoleId, int pageSize, int pageNo) throws PwtzDatabaseException
    {
        PaginationList<CompanyUser> paginationList = new PaginationList<CompanyUser>();
        
        if (_logger.isInfoEnabled())
        {
            _logger.info("selectUserByRole:|companyRoleId=" + companyRoleId);
        }
        List<CompanyUser> list = null;
        int benginIndex = pageSize * pageNo - pageSize;
        try
        {
            list = companyUserMapper.selectUserByRole(companyRoleId, benginIndex, pageSize);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (list!=null)
        {
            _logger.debug("selectUserByRole:result menu=" + list.toString());
        }
        int totalCount = 0;
        int totalPage =1;
        if(pageSize!=0){
            try
            {
                totalCount = companyUserMapper.selectCountUserByRole(companyRoleId);
            }
            catch (DataAccessException e)
            {
                _logger.error(ExceptionUtils.getStackTrace(e));
                throw new PwtzDatabaseException();
            }
            totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        }else{
            pageNo=1;
            if(list!=null && list.size()>0){
                totalCount = list.size();
            }
        }
        paginationList.setTotalPage(totalPage);
        paginationList.setCurrPage(pageNo);
        paginationList.setPageSize(pageSize);
        paginationList.setRecords(list);
        paginationList.setTotalRecord(totalCount);
        return paginationList;
    }
    
    @Override
    public PaginationList<CompanyUser> selectUseByLastCompanyId(long companyRoleId,long lastCompanyId, String loginName, int pageSize, int pageNo) throws PwtzDatabaseException
    {
        PaginationList<CompanyUser> paginationList = new PaginationList<CompanyUser>();
        
        if (_logger.isInfoEnabled())
        {
            _logger.info("selectCountUseByLastCompanyId:|lastCompanyId=" + lastCompanyId);
        }
        List<CompanyUser> list = null;
        int benginIndex = pageSize * pageNo - pageSize;
        try
        {
            list = companyUserMapper.selectUserByLastCompanyId(companyRoleId,lastCompanyId, loginName, benginIndex, pageSize);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (list!=null)
        {
            _logger.debug("selectCountUseByLastCompanyId:result menu=" + list.toString());
        }
        int totalCount = 0;
        int totalPage =1;
        if(pageSize!=0){
            try
            {
                totalCount = companyUserMapper.selectCountUseByLastCompanyId(companyRoleId,lastCompanyId, loginName);
                //System.out.println("selectCountUseByLastCompanyId="+totalCount);
            }
            catch (DataAccessException e)
            {
                _logger.error(ExceptionUtils.getStackTrace(e));
                throw new PwtzDatabaseException();
            }
            totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        }else{
            pageNo=1;
            if(list!=null && list.size()>0){
                totalCount = list.size();
            }
        }
        paginationList.setTotalPage(totalPage);
        paginationList.setCurrPage(pageNo);
        paginationList.setPageSize(pageSize);
        paginationList.setRecords(list);
        paginationList.setTotalRecord(totalCount);
        return paginationList;
    }
    
    
    @Override
    public PaginationList<CompanyUser> selectUseByCompanyId(long companyId, int pageSize, int pageNo) throws PwtzDatabaseException
    {
        PaginationList<CompanyUser> paginationList = new PaginationList<CompanyUser>();
        
        if (_logger.isInfoEnabled())
        {
            _logger.info("selectCountUseByLastCompanyId:|companyId=" + companyId);
        }
        List<CompanyUser> list = null;
        int benginIndex = pageSize * pageNo - pageSize;
        try
        {
            list = companyUserMapper.selectUserByCompanyId(companyId, benginIndex, pageSize);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (list!=null)
        {
            _logger.debug("selectCountUseByLastCompanyId:result menu=" + list.toString());
        }
        int totalCount = 0;
        int totalPage =1;
        if(pageSize!=0){
            try
            {
                totalCount = companyUserMapper.selectCountUseByCompanyId(companyId);
            }
            catch (DataAccessException e)
            {
                _logger.error(ExceptionUtils.getStackTrace(e));
                throw new PwtzDatabaseException();
            }
            totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
        }else{
            pageNo=1;
            if(list!=null && list.size()>0){
                totalCount = list.size();
            }
        }
        paginationList.setTotalPage(totalPage);
        paginationList.setCurrPage(pageNo);
        paginationList.setPageSize(pageSize);
        paginationList.setRecords(list);
        paginationList.setTotalRecord(totalCount);
        return paginationList;
    }
    
    @Override
    public int deleteRef(Long userId,Long companyId) throws PwtzDatabaseException
    {
        int result = 0;
        if (_logger.isInfoEnabled())
        {
            _logger.info("delete:userId=" + userId);
        }
        try
        {
            result = userCompanyRefMapper.deleteRef(userId,companyId);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return result;
    }
    
    @Override
    public int deleteCompanyUserRoleByUserId(long companyUserId, long companyRoleId) throws PwtzDatabaseException
    {
        int ret = 0;
        if (_logger.isDebugEnabled())
        {
            _logger.debug("deleteCompanyUserRoleByUserId:companyUserId=" + companyUserId + ",companyRoleId="+companyRoleId);
        }
        try
        {
            ret = companyUserRoleMapper.deleteCompanyUserRoleByUserId(companyUserId, companyRoleId);
        }
        catch (DataAccessException _e)
        {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new PwtzDatabaseException();
        }
        return ret;
    }

    @Override
    public int clearUserRoleRef(long companyUserId, long companyId) throws PwtzDatabaseException
    {
        int ret = 0;
        if (_logger.isDebugEnabled())
        {
            _logger.debug("clearUserRoleRef:companyUserId=" + companyUserId + ",companyId="+companyId);
        }
        try
        {
            ret = companyUserRoleMapper.clearUserRoleRefByCompanyId(companyUserId,companyId);
        }
        catch (DataAccessException _e)
        {
            _logger.error(ExceptionUtils.getStackTrace(_e));
            throw new PwtzDatabaseException();
        }
        return ret;
    }

    @Override
    public String getCompanyUserName(Long companyUserId, int type) throws PwtzDatabaseException
    {
        if (companyUserId == null || companyUserId == 0)
            return "";
        final String sql = "SELECT {0} FROM T_COMPANY_USER WHERE COMPANY_USER_ID=? AND STATUS=1";
        try
        {
            if (type == 1)
            {
                List<Map<String, Object>> objs = jdbcTemplate.queryForList(MessageFormat.format(sql, "LOGIN_NAME"), companyUserId);
                if (objs == null || objs.size() == 0)
                    return "";
                return objs.get(0).get("LOGIN_NAME").toString();
            }
            else if (type == 2)
            {
                List<Map<String, Object>> objs = jdbcTemplate.queryForList(MessageFormat.format(sql, "USER_NAME"), companyUserId);
                if (objs == null || objs.size() == 0)
                    return "";
                return objs.get(0).get("USER_NAME").toString();
            }
            else
            {
                List<Map<String, Object>> objs = jdbcTemplate.queryForList(MessageFormat.format(sql, "MOBILE"), companyUserId);
                if (objs == null || objs.size() == 0)
                    return "";
                return objs.get(0).get("MOBILE").toString();
            }
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
    }
    
    @Override
    public List<CompanyUser> selectUserByCompanyIds(Long companyId) throws PwtzDatabaseException
    {
        if (_logger.isInfoEnabled())
        {
            _logger.info("findCompanyUser:|companyId=" + companyId);
        }
        List<CompanyUser> list = null;
        String companyidStr = "";
        String[] companyids=null;
        companyidStr = companyService.findChildrensId(companyId,null);
        if(companyidStr!=null && companyidStr.length()>0){
            companyidStr= companyidStr.substring(0,companyidStr.length()-1);
            companyids =companyidStr.split(",");
        }
        try
        {
            list = companyUserMapper.selectUserByCompanyIds(companyids);
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (list!=null)
        {
            _logger.debug("findCompanyUser:result list=" + list.toString());
        }

        return list;
    }

    @Override
    public boolean exitCompany(Long companyUserId) throws PwtzDatabaseException
    {
        boolean result=false;
        if(_logger.isDebugEnabled()){
            _logger.debug("exitCompany:companyUserId="+companyUserId);
        }
        if(companyUserId==null){
            if(_logger.isDebugEnabled()){
                _logger.debug("exitCompany,companyUserId is null");
            }
            return result;
        }
        try{
            
        }catch(DataAccessException e){
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return false;
    }

    @Override
    public List<RandomCode> getCompanyRandomCode(Long companyId) throws PwtzDatabaseException
    {
        List<RandomCode> list=null;
        if(_logger.isDebugEnabled()){
            _logger.debug("getCompanyRandomCode: companyId="+companyId);
        }
        if(companyId==null||companyId==0){
            if(_logger.isDebugEnabled()){
                _logger.debug("getCompanyRandomCode,companyId is disabled");
            }
            return list;
        }
        try{
            list=randomCodeMapper.selectCompanyRandomCode(companyId);
        }catch(DataAccessException e){
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        return list;
    }

	@Override
	public PaginationList<CompanyUser> selectTmepUser(int pageNo, int pageSize)
			throws PwtzDatabaseException {
		 if (_logger.isInfoEnabled())
	        {
	            _logger.info("selectTmepUser:|pageNo=" + pageNo);
	        }
		 PaginationList<CompanyUser> paginationList = new PaginationList<CompanyUser>();
	      int benginIndex = pageSize * pageNo - pageSize;
		 List<CompanyUser> list = null;
		  try {
			list= companyUserMapper.selectTmepUser(benginIndex, pageSize);
		} catch (DataAccessException e) {
			  _logger.error(ExceptionUtils.getStackTrace(e));
	            throw new PwtzDatabaseException();
		}
		  if (list!=null)
	        {
	            _logger.debug("selectTmepUser  menu=" + list.toString());
	        }
	        int totalCount = 0;
	        int totalPage =1;
	        if(pageSize!=0){
	            try
	            {
	                totalCount = companyUserMapper.selectTmepUserCount();
	            }
	            catch (DataAccessException e)
	            {
	                _logger.error(ExceptionUtils.getStackTrace(e));
	                throw new PwtzDatabaseException();
	            }
	            totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;
	        }else{
	            pageNo=1;
	            if(list!=null && list.size()>0){
	                totalCount = list.size();
	            }
	        }
	        paginationList.setTotalPage(totalPage);
	        paginationList.setCurrPage(pageNo);
	        paginationList.setPageSize(pageSize);
	        paginationList.setRecords(list);
	        paginationList.setTotalRecord(totalCount);
		  
		return paginationList;
	}

	@Override
	public int selectTmepUserById(Long userid) throws PwtzDatabaseException {
		 if (_logger.isInfoEnabled())
	        {
	            _logger.info("selectTmepUserById:|userid=" + userid);
	        }
		int result = 0;
		try {
			result = companyUserMapper.selectTmepUserById(userid);
		} catch (DataAccessException e) {
			 _logger.error(ExceptionUtils.getStackTrace(e));
             throw new PwtzDatabaseException();
		}
		
		return result;
	}
}
